/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.drakeet.multitype.sample.payload;

import me.drakeet.multitype.MultiTypeAdapter;
import me.drakeet.multitype.sample.ResourceTable;

import me.drakeet.multitype.sample.utils.ViewCache;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;

import java.util.ArrayList;
import java.util.List;

/**
 * TestPayloadSlice
 */
public class TestPayloadSlice extends AbilitySlice {
    private static final int MAX_LENGTH = 50;

    private ComponentContainer rootContainer;

    private List<String> itemsList;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        rootContainer = (ComponentContainer) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_communicate_with_binder_layout, null, false);
        super.setUIContent(rootContainer);
    }

    @Override
    public void onActive() {
        super.onActive();
        initializeView();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * initializeView
     */
    private void initializeView() {
        itemsList = new ArrayList<>();
        for (int pos = 0; pos < MAX_LENGTH; pos++) {
            itemsList.add(String.valueOf(pos));
        }
        if (rootContainer.findComponentById(ResourceTable.Id_list_main) instanceof ListContainer) {
            ListContainer listContainer = (ListContainer) rootContainer.findComponentById(ResourceTable.Id_list_main);
            listContainer.setItemProvider(new MultiTypeAdapter<String>( itemsList) {
                @Override
                public Component getMyView(int position, Component convertView, ComponentContainer parent, String data) {
                    Component result = convertView;
                    if (result == null) {
                        result = LayoutScatter.getInstance(TestPayloadSlice.this)
                                .parse(ResourceTable.Layout_item_test_payload, parent, false);
                    }

                    ViewCache viewCache = (ViewCache) result.getTag();
                    if (viewCache == null) {
                        viewCache = new ViewCache(result);
                        result.setTag(viewCache);
                    }
                    viewCache.getLastTextView().setText("currentTimeMillis: " + System.currentTimeMillis());
                    viewCache.getDirectionalLayoutLine();
                    return result;
                }
            });
        }
    }
}